var CONSTS = require('../util/consts');
var moment = require('moment');
var async = require('async');
var Common = require('../util/common');
var CONFIG = require('../config/config').Config;
// var nodemailer = require('nodemailer');
var thenjs = require('thenjs');
var crypto = require("crypto");
var validator = require('validator');
var models = require('../models');
var fs = require('fs-extra');
var articles = require('./ghost.json')
var request = require('request-json');

var client = request.createClient('http://localhost:2368/');

// console.log(Object.keys(articles.db[0].data));
// console.log(JSON.stringify(articles.db[0].data.posts, ' ', 2));
// console.log(JSON.stringify(articles.db[0].data.tags, ' ', 2));
// console.log(JSON.stringify(articles.db[0].data.posts_tags, ' ', 2));

var tags = articles.db[0].data.tags;
var posts_tags = articles.db[0].data.posts_tags;
var posts2tags = {};
var id2tagObj = {};

for (var i = 0; i < tags.length; i++) {
    id2tagObj[tags[i].id] = tags[i].name;
}

for (var i = 0; i < posts_tags.length; i++) {
    var post_id = posts_tags[i].post_id;
    var tag_id = posts_tags[i].tag_id;

    if (typeof posts2tags[post_id] == 'undefined') {
        posts2tags[post_id] = [];
    }

    posts2tags[post_id].push(id2tagObj[tag_id]);
}

client.post('/api/user/login', {
    username: 'admin',
    password: '123456',
}, function(err, res, body) {
    var posts = articles.db[0].data.posts;

    for (var i = 0; i < posts.length; i++) {
        (function(idx) {
            var id = posts[idx].id;
            var htmlstr = posts[idx].html.replace(/<pre>/g, '').replace(/<code>/g, '').replace(/<\/pre>/g, '').replace(/<\/code>/g, '').replace(/\n\n/g, '\n').replace(/\n/g, '</p><p>');

            var html = '<p>' + htmlstr + '</p>';

            var markdown = posts[idx].markdown.replace(/<pre>/g, '').replace(/<code>/g, '').replace(/<\/pre>/g, '').replace(/<\/code>/g, '').replace(/\n\n/g, '\n').replace(/\n/g,'\n\n') + '\n';
            var article = new models.Article({
                author: body.msg._id,
                title: posts[idx].title,
                html: html,
                markdown: markdown,
                tags: posts2tags[id] ? posts2tags[id] : [],
                created: new Date(posts[idx].created_at),
                lastmod: new Date(posts[idx].created_at),
            });

            article.save(function(err, article) {
                if (err) {
                    console.log(err);
                    return;
                }

                if (posts2tags[id] != []) {
                    var ts = article.tags;
                    for (var j = 0; j < ts.length; j++) {
                        (function(jdx) {
                            var tag = new models.Tag({
                                author: body.msg._id,
                                article: article._id,
                                name: ts[jdx],
                                created: moment().utc().format()
                            });
                            tag.save(function(err, tag) {
                                if (err) {
                                    console.log(err);
                                    return;
                                }
                            });
                        }(j));
                    }
                }

                console.log("import " + idx + " successfully");
            });

        }(i));
    }
});